<?php
/**
 * 大转盘
 */
namespace Wap\Controller;

use Common\Model\EventsModel;
use Common\Model\TurnModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Wap\BaseController;

class TurnController extends BaseController {

    /**
     * 大转盘
     */
    public function index(){
        $id=I('get.mid',0,'intval');
        if (!$id){
            die('参数错误');
        }

        $uid=session('uid')+0;
        if(!UserModel::checkFollow($uid)){
            $this->smarty->display("tmp/focus.html");
            die;
        }

        $mapping=TurnModel::getMappingInstance()->where("id={$id}")->find();
        if (!$mapping){
            die('参数错误');
        }


        if($this->isWechat) {
            $domain=UtilsModel::getFullDomain();
            //微信分享相关
            $this->smarty->assign('isWxShare', true);

            if (strcasecmp($mapping['gotWay'],'BABY_BIRTHDAY')==0){
                $this->smarty->assign('wxTitle', "丸子妈妈生日专属福利");
                $this->smarty->assign('wxDesc',  "当月生日的丸宝，都拥有一次免费抽奖机会，奖品内容会不定期更新，当月生日的宝宝不要错过机会哦！");
            }else{
                $this->smarty->assign('wxTitle', "丸子妈妈每周大转盘福利");
                $this->smarty->assign('wxDesc',  "各种免费福利、玩具、红包送不停，丸子妈妈每周定期福利，等你来抢！");
            }


            $this->smarty->assign('wxLink', $domain.U('Turn/index?mid='.$id));
            $this->smarty->assign('wxImage', $domain.'/Public/Wap/images/logo.jpg');
            $this->smarty->assign('wxObjType',0);
            $this->smarty->assign('wxObjID',0);
        }


        $now=time();
        if($now<strtotime($mapping['applyStartTime'])){
            $this->smarty->assign('fail',"对不起，活动尚未开始");
        }elseif($now>=strtotime($mapping['applyEndTime'])){
            $this->smarty->assign('fail',"对不起，活动已经结束");
        }



        if( $mapping['pointFee']>0) {
            $uret = UserModel::getUserChangeful($uid);
            if ($uret['status'] != 0) {
                die($uret['msg']);
            }
            $this->smarty->assign('pointFee', $mapping['pointFee']);
            if ($uret['data']['point'] < $mapping['pointFee']) {
                $this->smarty->assign('pointLack',1);
            }
        }


        ///////////////////////////////////////////////////////////////////
        switch ($mapping['gotWay']){
            case TurnModel::GOTWAY_APPLY://报名，手动填入名单
                $chance=TurnModel::getChanceInstance()->where("mid={$id} and uid={$uid} and used=0")->find();
                if(!$chance){
                    $this->smarty->assign('fail',"对不起，您没有抽奖机会");
                }
                break;
            case TurnModel::GOTWAY_NEW://拉新,指定活动完成报名后，即获得一次抽奖机会
                if(TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->find()){
                    $this->smarty->assign('fail',"您已参与过本次抽奖");
                }
                if(EventsModel::getJoinInstance()->where("eid={$mapping['active']} and uid={$uid} and status!=".EventsModel::JOIN_STATUS_CANCELAPPLY)->find()){
                    //已经报名
                }else{
                    //未报名
                    $eventName=EventsModel::getEventInstance()->where("eid={$mapping['active']}")->getField('title');
                    $this->smarty->assign('fail',"只有报名{$eventName}，才可以参与抽奖");
                }
                break;
            case TurnModel::GOTWAY_SIGN://签到,即打开指定链接，即可获得一次抽奖次数
                if(TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->find()){
                    $this->smarty->assign('fail',"您已参与过本次抽奖");
                }
                break;
            case TurnModel::GOTWAY_BABYBIRTHDAY:
                $babyBirthday1=M('tmp_babyinfo',null,DB_MAIN_CFG)->where("uid={$uid}")->getField("babyBirthday1");
                if (!$babyBirthday1){
                    $this->smarty->assign('fail',"您尚未填写宝贝的生日");
                    $this->smarty->assign('jump',true);
                    break;
                }
                $birthTimestamp = strtotime($babyBirthday1);
                $month      = date('n',$birthTimestamp);
                $now = time();
                $nowMonth   = date('n',$now);
                if (strcasecmp($month,$nowMonth)!=0){
                    $this->smarty->assign('fail',"非本月生日，不能参与抽奖");
                    break;
                }

                $lastTime=TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->max('addTime');
                if ($lastTime && strcasecmp(date('Yn',$now),date('Yn',$lastTime))==0){
                    $this->smarty->assign('fail',"今年已经参加过抽奖");
                    break;
                }

                break;
        }
        ///////////////////////////////////////////////////////////////////


        $items=TurnModel::getItemInstance()->where("setId={$mapping['turn']}")->select();
        $this->smarty->assign('prizes',$items);
        $this->smarty->assign('mid',$id);


        $log=TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->field("prizeNote,addTime")->order("id desc")->limit(3)->select();
        if (count($log)>0) {
            $logstr='';
            foreach ($log as $one) {
                $logstr .=date('m-d H:i',$one['addTime']).'&nbsp;&nbsp;&nbsp;&nbsp;'.$one['prizeNote'].'<br />';
            }
            $this->smarty->assign('log', $logstr);
        }

        $rule=preg_replace('/\r|\n/','',$mapping['rule']);
        $this->smarty->assign('rule', $rule);

        $this->smarty->display("tmp/turn.html");

    }

    /**
     * 大转盘启动抽奖
     */
    public function open(){
        $id=I('get.mid',0,'intval');
        $uid=session('uid');
        $mapping=TurnModel::getMappingInstance()->where("id={$id}")->find();
        if (!$mapping){
            $this->ajaxReturn(array('status'=>1,'msg'=>'参数错误'));
        }    
        
        switch ($mapping['gotWay']){
            case TurnModel::GOTWAY_APPLY://报名，手动填入名单
                $chance=TurnModel::getChanceInstance()->where("mid={$id} and uid={$uid} and used=0")->find();
                if(!$chance){
                    $this->ajaxReturn(array('status'=>1,'msg'=>"对不起，您没有抽奖机会"));
                }
                TurnModel::getChanceInstance()->where("id={$chance['id']}")->setField('used',1);
                break;
            case TurnModel::GOTWAY_NEW://拉新,指定活动完成报名后，即获得一次抽奖机会
                if(TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->find()){
                    $this->ajaxReturn(array('status'=>1,'msg'=>'您已参与过本次抽奖'));
                }
                if(EventsModel::getJoinInstance()->where("eid={$mapping['active']} and uid={$uid} and status!=".EventsModel::JOIN_STATUS_CANCELAPPLY)->find()){
                    //已经报名
                }else{
                    //未报名
                    $eventName=EventsModel::getEventInstance()->where("eid={$mapping['active']}")->getField('title');
                    $this->ajaxReturn(array('status'=>1,'msg'=>"只有报名{$eventName}，才可以参与抽奖"));
                }
                break;
            case TurnModel::GOTWAY_SIGN://签到,即打开指定链接，即可获得一次抽奖次数
                if(TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->find()){
                    $this->ajaxReturn(array('status'=>1,'msg'=>'您已参与过本次抽奖'));
                }
                break;
            case TurnModel::GOTWAY_BABYBIRTHDAY:
                $babyBirthday1=M('tmp_babyinfo',null,DB_MAIN_CFG)->where("uid={$uid}")->getField("babyBirthday1");
                if (!$babyBirthday1){
                    $this->ajaxReturn(array('status'=>1,'msg'=>'您尚未填写宝贝的生日'));
                    break;
                }
                $birthTimestamp = strtotime($babyBirthday1);
                $month      = date('n',$birthTimestamp);
                $now = time();
                $nowMonth   = date('n',$now);
                if (strcasecmp($month,$nowMonth)!=0){
                    $this->ajaxReturn(array('status'=>1,'msg'=>'非本月生日，不能参与抽奖'));
                    break;
                }

                $lastTime=TurnModel::getJoinInstance()->where("mid={$id} and uid={$uid}")->max('addTime');
                if ($lastTime && strcasecmp(date('Yn',$now),date('Yn',$lastTime))==0){
                    $this->ajaxReturn(array('status'=>1,'msg'=>'今年已经参与过抽奖'));
                    break;
                }

                break;
        }
        if ($mapping['pointFee']>0){
            $uret=UserModel::getUserChangeful($uid);
            if ($uret['status']!=0){
                $this->ajaxReturn($uret);
            }
            if ($uret['data']['point']<$mapping['pointFee']){
                $this->ajaxReturn(array('status'=>1,'msg'=>"本次抽奖需要{$mapping['pointFee']}积分，当前您的积分不足"));
            }
        }
        $result=TurnModel::getLuckyResult($uid,$id,$mapping['turn'],$mapping['active'],$mapping['pointFee']);
        if ($mapping['pointFee']>0 && $result['status']==TurnModel::SUCCESS){
            UserModel::minusPoint($uid,$uret['data']['point'],$mapping['pointFee'],OPT_TURN,$id);
        }
        $this->ajaxReturn($result);

    }





}